package com.example.rocketmq;

import code.example.tool.LogFactory;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

public class MyConsumer {

    private String topic ="MqTopic";
    private String tag = "mqTag";
    private String group = "Test";
    public void start()  {
        try {
            DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer(group);
            defaultMQPushConsumer.setNamesrvAddr("localHost:9876");
            defaultMQPushConsumer.subscribe(topic, "*");
            defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {
                @Override
                public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                    LogFactory.getLog().info(msgs.toString());
                    return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
                }
            });
            defaultMQPushConsumer.start();
            LogFactory.getLog().info(" start consumer success");
        }catch (Exception e){
            LogFactory.getLog().error(" start consumer fail:",e);
        }
    }
    public static void main(String args []){
        MyConsumer myConsumer = new MyConsumer();
        myConsumer.start();
        MyProducer myProducer = new MyProducer();
        myProducer.start();

    }
}
